local battleSceneLoader = require "Battle/BattleSceneLoader"
BattleMainUI = { }
local this = BattleMainUI

function BattleMainUI.init()
    this.gameObject = battleSceneLoader.loadUI("BattleMainUI")
    this.temUnit = UnityUtil.GetChildBone(this.gameObject.transform, "temUnit")
    this.temUnit.gameObject:SetActive(false)
    this.units = { }
    table.insert(this.units, this.createUint(1))
    table.insert(this.units, this.createUint(2))
    table.insert(this.units, this.createUint(3))
    table.insert(this.units, this.createUint(4))
    table.insert(this.units, this.createUint(5))
end

function BattleMainUI.createUint(index)
    local unit = { }
    unit.rectTransform = UnityEngine.GameObject.Instantiate(this.temUnit)
    unit.rectTransform:SetParent(this.temUnit.parent)
    unit.rectTransform.localScale = Vector3.New(1,1,1)
    unit.unitData = RoleManager.team[index]
    unit.anchoredPosition3D = this.temUnit.anchoredPosition3D
    unit.anchoredPosition3D.x = unit.anchoredPosition3D.x + 180 *(index - 3)
    unit.rectTransform.anchoredPosition3D = unit.anchoredPosition3D
    unit.button = UnityUtil.GetChildBone(unit.rectTransform, "Button")
    unit.button = unit.button:GetComponent(typeof(LuaButtonComponent))
    unit.textHP = UnityUtil.GetChildBone(unit.rectTransform, "TextHP")
    unit.textHP = unit.textHP:GetComponent(typeof(UnityEngine.UI.Text))
    unit.sliderHP = UnityUtil.GetChildBone(unit.rectTransform, "SliderHP")
    unit.sliderHP = unit.sliderHP:GetComponent(typeof(UnityEngine.UI.Slider))
    unit.textName = UnityUtil.GetChildBone(unit.rectTransform, "TextName")
    unit.textName = unit.textName:GetComponent(typeof(UnityEngine.UI.Text))
    unit.textMP = UnityUtil.GetChildBone(unit.rectTransform, "TextMP")
    unit.textMP = unit.textMP:GetComponent(typeof(UnityEngine.UI.Text))
    unit.sliderMP = UnityUtil.GetChildBone(unit.rectTransform, "SliderMP")
    unit.sliderMP = unit.sliderMP:GetComponent(typeof(UnityEngine.UI.Slider))
    unit.rectTransform.gameObject:SetActive(true)
    --    GameLog.Error(unit.button)
    --    GameLog.Error(unit.unitData)
    --    GameLog.Error(unit.sliderHP)
    --    GameLog.Error(unit.sliderMP)
    unit.textName.text = unit.unitData.name
    unit.textHP.text = unit.unitData.hp
    unit.sliderHP.value = 1
    unit.textMP.text = unit.unitData.mp
    unit.sliderMP.value = 0

    unit.unitData.eventHPChange:Add( function()
        --        GameLog.Error(unit.unitData.hp)
        unit.textHP.text = unit.unitData.hp
        unit.sliderHP.value = unit.unitData.hp / unit.unitData.hpMax
    end )

    unit.unitData.eventMPChange:Add( function()
        --        GameLog.Error(unit.unitData.mp)
        unit.textMP.text = unit.unitData.mp
        unit.sliderMP.value = unit.unitData.mp / unit.unitData.mpMax
        if (unit.unitData.isMPFull) then
            unit.textName.text = unit.unitData.name .. "\n" .. "可以释放奥义"
        else
            unit.textName.text = unit.unitData.name
        end
    end )

    unit.button:AddBtnClickGameObject( function(args)
        --        GameLog.Error("OnMPFullClick")
        if (unit.unitData.isMPFull) then
            --unit.textName.text = unit.unitData.name .. "\n" .. "可以释放奥义"
            GameLog.Warning("click aoyi " .. unit.unitData.name)
            unit.unitData:castAoyi()
        end
    end , unit.button.gameObject)
    return unit
end


function BattleMainUI.clear()
    UnityUtil.Destroy(this.gameObject)
    this.gameObject = nil
    this.units = nil
end


